<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#">
  <!ENTITY dc "http://purl.org/dc/elements/1.1/">
  <!ENTITY dcterms "http://purl.org/dc/terms/">
  <!ENTITY foaf "http://xmlns.com/foaf/0.1/">
  <!ENTITY a "http://xmlns.heustics.org/schema/myelin/0.1/">
  <!ENTITY m "http://xmlns.heustics.org/ontology/myelin/0.1/">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:l="http://xmlns.heustics.org/schema/i18n/0.1/"
  xmlns:a="http://xmlns.heustics.org/schema/myelin/0.1/"
  xmlns:fhttp="http://xmlns.4suite.org/4ss/http"
  xmlns:o="http://xmlns.heustics.org/schema/myelin/0.1/UserAccount#activate"
  xmlns:xf="http://www.w3.org/2002/xforms"
  xmlns:email="http://xmlns.4suite.org/4ss/modules/e-mail"
  xmlns:u="&a;UserAccount#"
  xmlns:p="&a;AgentAccount#"
  extension-element-prefixes="fhttp email"
  exclude-result-prefixes="xsl a l fhttp o xf u p"
>  
  <xsl:variable name="o:l10n" select="a:get-l10n('&a;UserAccount#activate')"/>
  <xsl:variable name="o:titles-l10n" select="$o:l10n/l:context[@name='titles']/*"/> 
  <xsl:variable name="o:params-l10n" select="$o:l10n/l:context[@name='params']/*"/>
  <xsl:variable name="o:hints-l10n" select="$o:l10n/l:context[@name='param-hints']/*"/>
  
  <xsl:template match="a:title[@key='activate']" mode="a:head-content">
    <xsl:call-template name="a:head-title">
      <xsl:with-param name="title" select="$o:titles-l10n[1]"/>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="a:title[@key='activate']" mode="a:page-header">
    <xsl:call-template name="a:page-header-title">
      <xsl:with-param name="title" select="$o:titles-l10n[1]"/>
    </xsl:call-template>
  </xsl:template>     
   
  <xsl:template match="a:nick" mode="a:input-show"> 
    <xsl:call-template name="p:nick-input">
      <xsl:with-param name="params-l10n" select="$u:params-l10n"/>
      <xsl:with-param name="hints-l10n" select="$u:hints-l10n"/>  
    </xsl:call-template>
  </xsl:template>
    
  <xsl:template match="a:activation-key" mode="a:input-show">        
    <div class='input text activation'>  
      <div class="key">
        <div>
          <xsl:call-template name="a:label-class-attr"/>
          <span><xsl:value-of select="$o:params-l10n[@key='activation-key']"/></span>
          <div class="spacer"/>
        </div>        
        <div class='hint'>
          <xsl:call-template name="a:input-hints">
            <xsl:with-param name="hint" select="$o:hints-l10n[@key='activation-key']"/>
          </xsl:call-template>
          <div class="spacer"/>
        </div>
      </div>      
      <div class='value'>
        <input class='max' type="text" value="{.}" name="activation-key"/>
      </div>
      <div class='spacer'/>
    </div>
  </xsl:template>  
 
  <xsl:template match="a:submit[@key='commit']">    
    <xsl:param name="name"/>
    <xsl:param name="l10n"/>
    <xsl:param name='key' select='@key'/>
    <div class="input submit">
      <xsl:variable name='val' select='$l10n[@key="activate"]'/>
      <input type='hidden' name='{$key}' value='{$val}'/>
      <input type='submit' name='{$name}' value='{$val}'/>
      <div class='spacer'/>
    </div>        
  </xsl:template> 
  
  <xsl:template match="a:submit[@key='close']">    
    <xsl:param name="name"/>
    <xsl:param name="l10n"/>
    <xsl:param name='key' select='@key'/>
    <div class="input submit">
      <xsl:variable name='val' select='$l10n[@key="cancel"]'/>
      <input type='hidden' name='{$key}' value='{$val}'/>
      <input type='submit' name='{$name}' value='{$val}'/>
      <div class='spacer'/>
    </div>        
  </xsl:template> 
  
  <xsl:template match="a:form[@status='init' and @msg='new-activate']" mode="a:form-header" name="a:form-header">
    <xsl:param name="msg" select="@msg"/>
    <xsl:param name="l10n" select="$o:l10n/l:context[@name='form-status']"/>
    <div class="entry status">
      <span><xsl:value-of select="$l10n/*[@key='new-activate-init']"/></span>
      <div class="spacer"/>
    </div>
  </xsl:template>  
 
  <xsl:template match="a:form[@mode='main' and @status='commit-success']">
    <xsl:param name="config" select="/*/a:config"/>
    <xsl:param name="account" select="/*/a:account"/>
    <xsl:param name="action" select="/*/@action"/>
    <xsl:param name="return-action" select="/*/@return-action"/>
    <xsl:variable name="email-l10n" select="$o:l10n/l:context[@name='account-activated-email']/*"/>
    <xsl:variable name="inst" select="xf:model/xf:instance/*"/>
    <xsl:variable name="agent" select="$inst/a:nick"/>
    <email:send-mail 
      to="{$agent/@name} &lt;{substring($agent/@mbox,8)}&gt;"
      from="{$config/@email-from}"
      subject="{$email-l10n[1]}"
      method="html"
      media-type="text/html"
      server="{$config/@smtp-gateway}">          
      <html>
        <body>        
          <xsl:apply-templates select="$inst" mode='a:account-activated-email-body'>          
            <xsl:with-param name="config" select="$config"/>
            <xsl:with-param name="email-l10n" select="$email-l10n"/>
            <xsl:with-param name="agent" select="$agent"/>
          </xsl:apply-templates>
        </body>
      </html>     
    </email:send-mail>
    <xsl:choose>
      <xsl:when test="$account/@logged-in">
        <xsl:apply-templates select="$inst" mode="a:account-activated">
          <xsl:with-param name="config" select="$config"/>
          <xsl:with-param name="account" select="$account"/>
          <xsl:with-param name="target" select="@target"/>
          <xsl:with-param name="target-action" select="@target-action"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:when test="$action != @target-action">
        <xsl:choose>
          <xsl:when test="$action != $return-action">
            <fhttp:response-uri
              uri="{a:format-url-full-secure(
                a:format-url-ext(
                  a:format-url-target-action(
                    a:format-url-target(
                      a:format-repeat-request('login'),
                      @target
                    ),
                    @target-action
                  ),
                  'msg','activation-success'
                )
              )}"/>
          </xsl:when>
          <xsl:otherwise>
            <fhttp:response-uri
              uri="{a:format-url-full-secure(
                a:format-url-ext(
                  a:format-url-target-action(
                    a:format-url-target(
                      a:format-root-url('login'),
                      @target
                    ),
                    @target-action
                  ),
                  'msg','activation-success'
                )
              )}"/>
          </xsl:otherwise>
        </xsl:choose>        
      </xsl:when>
      <xsl:when test="$action != $return-action">
        <fhttp:response-uri
          uri="{a:format-url-full-secure(
            a:format-url-ext(
              a:format-url-target(
                a:format-repeat-request('login')
              ),             
              'msg','activation-success'
            )
          )}"/>
      </xsl:when>
      <xsl:otherwise>
        <fhttp:response-uri
          uri="{a:format-url-full-secure(
            a:format-url-ext(
              a:format-root-url('login'),
              'msg','activation-success'
            )
          )}"/>
      </xsl:otherwise>
    </xsl:choose>   
  </xsl:template>
  
  <xsl:template match="a:user-account" mode="a:account-activated-email-body">   
    <xsl:param name="email-l10n"/> 
    <xsl:param name="config" select="/*/a:config"/>   
    <xsl:variable name="agent" select="a"/>
    <p><xsl:value-of select="concat($email-l10n[2],$agent/@name)"/></p>
    <p><xsl:value-of select="$email-l10n[3]"/></p>
    <ul>
      <li><xsl:value-of select="concat($email-l10n[4],$agent)"/></li>
      <!-- li><xsl:value-of select="concat($email-l10n[5],' ',a:password)"/></li -->
    </ul>
    <p>
      <xsl:value-of select="$email-l10n[6]"/>     
    </p>   
    <p>
      <xsl:variable name="link" select="a:format-url-min(a:public-url-full-secure($config/@root-uri),'login')"/>
      <a href='{$link}'>
        <xsl:value-of select="$link"/>
      </a>
    </p>
  </xsl:template>
  
  <xsl:template match="a:user-account" mode="a:account-activated">
    <xsl:param name="l10n" select="$o:l10n/l:context[@name='account-activated']/*"/>
    <xsl:param name="target"/>
    <xsl:param name="target-action"/>
    <div class="grouping">
      <p>
        <xsl:value-of select="$l10n[@key='main']"/>
      </p>
      <p>
        <a href="{a:format-repeat-request($target-action,$target)}">
          <xsl:value-of select="$l10n[@key='return']"/>
        </a>
      </p>
      <div class="spacer"/>
    </div>
  </xsl:template>
  
</xsl:stylesheet>

